function notify(title, content, logPath) {
    if (!logPath) {   
        logPath = "./log.html";
    }
    console.log("logPath: " + logPath);
    files.create(logPath);
    let data = `<html><body><div>${title}\n${content}<div></body></html>`;
    
    data = data.replace(/\n/g, "<br/>\n")
    files.write(logPath, data);     

    let manager = context.getSystemService(android.app.Service.NOTIFICATION_SERVICE);
    let intent = android.app.PendingIntent.getActivity(context, 0, app.intent({
        action: "VIEW",
        type: "text/html",
        data: "file://com.android.htmlfileprovider" + logPath,
        packageName: "com.android.browser",
        className: "com.android.browser.BrowserActivity"
      }), 0);
    var notification;
    if (device.sdkInt >= 26) {
        let channel = new android.app.NotificationChannel("channel_id", "京豆多合一签到", android.app.NotificationManager.IMPORTANCE_DEFAULT);
        channel.enableLights(true);
        channel.setLightColor(0xff0000);
        channel.setShowBadge(true);        
        channel.setLockscreenVisibility(android.app.Notification.VISIBILITY_PRIVATE);
        manager.createNotificationChannel(channel);
        notification = new android.app.Notification.Builder(context, "channel_id")
            .setContentTitle(title)
            .setContentText(content)
            .setStyle(new android.app.Notification.BigTextStyle().bigText(content))
            .setWhen(java.lang.System.currentTimeMillis())
            .setSmallIcon(org.autojs.autojs.R.drawable.autojs_material)
            .setAutoCancel(true)
            .setContentIntent(intent)
            .build();
    } else {
        notification = new android.app.Notification.Builder(context)
            .setContentTitle(title)
            .setContentText(content)
            .setWhen(new Date().getTime())
            .setAutoCancel(true)
            .setSmallIcon(org.autojs.autojs.R.drawable.autojs_material)
            .setContentIntent(intent)
            .build();
    }
    let notifyId = new Date().getTime() / 1000;
    manager.notify(notifyId, notification);
    return notifyId;
}

function checkLogin() {
    try {
        let cookieManager = android.webkit.CookieManager.getInstance();
        let url = "https://plogin.m.jd.com/cgi-bin/ml/islogin";
        let cookieStr = cookieManager.getCookie(url);
        if (!cookieStr) {
            return false;
        }
        let response = http.get(url, {
            headers: {
                cookie: cookieStr
            }
        });
        let json = response.body.json();
        return json.islogin == '1';
    } catch (e) {
        console.trace(e);
        return false;
    }
}

function openLoginPage() {
    try {
        toastLog("请登录京东");
        let exectuion = engines.execScriptFile("./jdLogin.js", {delay: 1000});
        log("唤起脚本")
        let waitTask = setInterval(() => {}, 1000);
        events.broadcast.on("jdLoginSuccess", () => {
            try {
                clearInterval(waitTask);
                let cookieManager = android.webkit.CookieManager.getInstance();
                let cookieStr = cookieManager.getCookie("https://api.m.jd.com/client.action");
                let saveCookieFile = files.cwd() + "/JD_Cookie.txt";
                files.create(saveCookieFile);
                files.write(saveCookieFile, cookieStr);
                toastLog("保存cookie成功，自动关闭登录页。请重新执行聚合签到");
                exectuion.getEngine().forceStop();
            } catch (e) {
                console.trace(e);
            }
        });
    } catch (e) {
        console.trace(e);
    }
}

// notify("京豆多合一签到", "领取到0个京豆");

module.exports = {
    notify: notify,
    checkLogin: checkLogin,
    openLoginPage: openLoginPage
}